package com.open.party.cloud.view.mine

import android.os.Bundle
import android.view.View
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusTitleBaseActivity
import cn.android.x.utils.ImmSolftManager
import com.open.party.cloud.R
import com.open.party.cloud.model.data.LocalCache
import com.open.party.cloud.view.login.WelcomeActivity
import com.open.party.cloud.viewModel.BaseViewModel
import com.sinothk.android.utils.XUtils
import kotlinx.android.synthetic.main.activity_user_edit.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


/**
 */
class UserEditActivity : AppEventBusTitleBaseActivity(), View.OnClickListener {

    private var viewModel: BaseViewModel? = null

    override fun getLayoutResId(): Int = R.layout.activity_user_edit

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("信息修改")

        viewModel = BaseViewModel()

        initView()
        initListener()
    }

    private fun initView() {
        val user = LocalCache.getLoginUser()
        nameEt.setText(XUtils.string().getNotNullValue(user.name))
        phoneEt.setText(XUtils.string().getNotNullValue(user.mainmobile))
    }

    private fun initListener() {
        submitBtn.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        ImmSolftManager(this).showKeyboard(false)
        when (v) {
            submitBtn -> {
                submit()
            }
        }
    }

    var name = ""
    private fun submit() {
        if (!XUtils.net().isConnected) {
            XUtils.toast().show("当前网络不可用")
            return
        }

        name = nameEt.text.toString()
        if (XUtils.string().isEmpty(name)) {
            XUtils.toast().show("请输入姓名")
            return
        }

//        val phone = phoneEt.text.toString()
//        if (XUtils.string().isEmpty(phone)) {
//            XUtils.toast().show("请输入电话号码")
//            return
//        } else if (phone.length != 11) {
//            XUtils.toast().show("电话号码长度应在11位")
//            return
//        }

        showLoadingDialog("正在提交")
        viewModel?.updateUserInfo(name)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<String>) {

        if ("updateUserInfo" != result.eventType) {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                XUtils.toast().show("修改成功")

                val user = LocalCache.getLoginUser();
                user.name = name
                LocalCache.setLoginUser(user)
                finish()
//                LocalCache.logout()
//                XUtils.page().finishAll()
//                XUtils.intent().openActivity(this@UserEditActivity, WelcomeActivity::class.java)
//                    .start()
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }
}